﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Deeper.DataAccess;
using Deeper.Common;
using System.Text.RegularExpressions;

namespace CodeBuilder
{
	public class builder
	{

		/// <summary> 
		/// 生成实体类
		/// </summary>
		/// <param name="table">表信息</param>
		/// <param name="projectNamespace">项目命名空间</param>
		/// <param name="ModelNamespace">Model层命名空间</param>
		/// <param name="AuthorName">作者</param>
		/// <param name="templateName">模板文件名称</param>
		/// <returns>实体层代码</returns>
		public static string BuilderModel(HTableAttribute table, string projectNamespace, string ModelNamespace, string ServiceNamespace, string AuthorName, string templateName)
		{
			ClassInfo classModel = new ClassInfo
			{
				AuthorName = AuthorName,
				Name = table.EntityName,
				NameSpace = string.Format("{0}.{1}", projectNamespace, ModelNamespace),
				Prefix = "public partial",
			};

			//ConfigHelper.GetConfigString("TemplateName")
			//读取模板文件
			string Template = FileHelper.ReadTextFile(templateName, Encoding.Default);
			//替换表名
			Template = Template.Replace("@TableName@", table.TableName);
			///替换主键字段
			Template = Template.Replace("@Pid@", table.PrimaryKey[0].ColumnName);
			///替换项目名
			Template = Template.Replace("@ProjectName@", projectNamespace);
			///替换命名空间
			Template = Template.Replace("@ModelNameSpace@", ModelNamespace);

			Template = Template.Replace("@ServiceNameSpace@", ServiceNamespace);
			///替换类名
			Template = Template.Replace("@ClassName@", classModel.Name);
			///替换作者名
			Template = Template.Replace("@AuthorName@", AuthorName);
			///替换创建时间
			Template = Template.Replace("@Datetime@", DateTime.Now.ToString("yyyy-MM-dd hh:mm"));
			///替换字段声明
			Template = Template.Replace("@Fields@", classModel.OutPutClass());
			///替换类注释
			Template = Template.Replace("@ClassSummary@", table.Remake);

			// string reg = "^@FieldsForeach@.*@EndField@";

			Template = EachColumns(Template, table.Columns.Cast<HColumnAttribute>().ToList(), "@FieldsForeach@", "@EndField@");

			Template = EachColumns(Template, table.Columns.Cast<HColumnAttribute>().Where(s => !s.IsPrimary), "@NoPrimaryFieldsForeach@", "@NoPrimaryEndField@");

			return Template;
		}

		private static string EachColumns(string Template, IEnumerable<HColumnAttribute> columns, string startTag, string endTag)
		{
			int pIndex = Template.IndexOf(startTag);
			while (pIndex > 0)
			{
				int eIndex = Template.IndexOf(endTag);
				if (eIndex < 0 || eIndex < pIndex) throw new Exception("模板格式不正确，FieldsForeach必须同EndField成对出现");
				string columnsInfo = Template.Substring(pIndex, eIndex - pIndex + endTag.Length);
				StringBuilder sb = new StringBuilder();
				foreach (HColumnAttribute dc in columns)
				{
					string a = columnsInfo.Replace("@CName@", dc.ColumnName)
						.Replace("@CRemake@", dc.Remake)
						.Replace("@DbType@", dc.DataType)
						.Replace("@IsPK@", dc.IsPrimary ? "true" : "false")
						.Replace("@IsId@", dc.IsIdentity ? "true" : "false")
						.Replace("@CLen@", dc.Length.ToString())
						.Replace("@IsNull@", dc.IsNullable ? "true" : "false")
						.Replace("@IsUniqueness@", dc.IsUniqueness ? "true" : "false")
						.Replace("@Type@", dc.ValueTypeName)
						.Replace("@CMemo@", dc.Remake)
						.Replace("@HColumnInfo@", dc.ToString())
						.Replace(startTag, "")
						.Replace(endTag, "");


					sb.AppendLine(a);
				}
				Template = Template.Replace(columnsInfo, sb.ToString());
				pIndex = Template.IndexOf(startTag);
			}
			return Template;
		}
	}
}

